# coding=utf-8
import sys
import os
from Base.base_request import request
from Util.handle_header import get_header
from Util.handle_result import handle_result, get_result_json, handle_result_json
from Util.handle_token import get_token, write_token, get_token_json
from Util.handle_excel import excel_data
import json
from Util.codition_data import get_data
# from collections.abc import Iterable


base_path = os.getcwd()
sys.path.append(base_path)


class RunMain:

    def run_case(self, index=None):
        rows = excel_data.get_rows(index)

        for i in range(rows):
            header = None
            depend_data = None
            data = excel_data.get_rows_value(i + 2, index)

            # print(data)
            is_run = data[2]
            if is_run == 'yes':
                is_depend = data[3]
                json_params = json.loads(data[7])
                if is_depend:
                    '''
                    获取依赖数据
                    依赖模块
                    '''
                    depend_key = data[4]
                    depend_data = get_data(is_depend)
                    # print(depend_data)
                    json_params[depend_key] = depend_data

                method = data[6]
                url = data[5]

                is_header = data[9]
                excepect_method = data[10]
                excepect_result = data[11]
                # print(type(excepect_result))
                token_method = data[8]
                if is_header == "yes":

                    if token_method == 'yes':
                        res_token = get_token()
                        write_token("Bearer " + res_token, "Authorization")
                        header = get_token_json()

                    elif token_method == None:
                        header = get_token_json()
                    else:
                        header = get_header()

                res = request.run_main(method, url, json_params, header)
                # print(res.get("data"))

                # # print(res)
                res_code = str(res.get('code'))
                message = res.get('message')
                if excepect_method == 'mec':
                    config_message = handle_result(url, res_code)
                    if message == config_message:
                        excel_data.excel_write_data(i + 2, 13, "通过")
                    else:
                        excel_data.excel_write_data(i + 2, 13, "失败")
                        excel_data.excel_write_data(i + 2, 14, json.dumps(res, ensure_ascii=False))

                if excepect_method == 'ExcepectCode':
                    if excepect_result == res_code:
                        excel_data.excel_write_data(i + 2, 13, "通过")
                    else:
                        excel_data.excel_write_data(i + 2, 13, "失败")
                        excel_data.excel_write_data(i + 2, 14, json.dumps(res, ensure_ascii=False))

                if excepect_method == 'json':
                    if res_code == '0':
                        status_str = 'data'
                    else:
                        status_str = 'error'
                    excepect_result = get_result_json(url, status_str)
                    result = handle_result_json(res.get("data"), excepect_result)

                    if result:
                        excel_data.excel_write_data(i + 2, 13, "通过")
                    else:
                        excel_data.excel_write_data(i + 2, 13, "失败")
                        excel_data.excel_write_data(i + 2, 14, json.dumps(res.get("data"), ensure_ascii=False))


if __name__ == "__main__":
    run = RunMain()
    run.run_case(1)
